package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CompanyResult;
import com.itheima.common.Result;
import com.itheima.mapper.BsUserMapper;
import com.itheima.mapper.SubjectsMapper;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.hm.HmSubjects;
import com.itheima.pojo.hm.HmSubjectsTags;
import com.itheima.pojo.hm.SubjectDto;
import com.itheima.service.SubjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*
 *@Parm
 *@ClassName SubjectsServiceImpl
 *@Date 2022/1/10 11:25
 */
@Service
public class SubjectsServiceImpl implements SubjectsService {
    @Autowired
    private SubjectsMapper subjectsMapper;
    @Override
    public CompanyResult updateById(HmSubjects subjects) {
        int i = subjectsMapper.updateById(subjects);
        CompanyResult companyResult = new CompanyResult();
        if(i <= 0){
            companyResult.setSuccess(false);
        }
        companyResult.setSuccess(true);
        return companyResult;
    }

    @Override
    public CompanyResult deleteById(Integer id) {
        int i = subjectsMapper.deleteById(id);
        CompanyResult companyResult = new CompanyResult();
        if(i <= 0){
            companyResult.setSuccess(false);
        }
        companyResult.setSuccess(true);
        return companyResult;
    }

    @Override
    public CompanyResult add(HmSubjects subjects) {
        subjects.setAddDate(LocalDateTime.now());
        Long userId = (Long) redisTemplate.opsForValue().get("userId");
        subjects.setCreatorID(Math.toIntExact(userId));
        subjects.setTags(0);
        subjects.setTotals(0);
        subjects.setTwoLevelDirectory(0);
        int insert = subjectsMapper.insert(subjects);
        CompanyResult companyResult = new CompanyResult();
        if(insert <= 0){
            companyResult.setId(0);
        }
        companyResult.setId(subjects.getId());
        return companyResult;
    }

    @Override
    public List<SubjectDto> findAll() {
        List<HmSubjects> hmSubjects = subjectsMapper.selectList(null);
        ArrayList<SubjectDto> subjectDtos = new ArrayList<>();
        for (HmSubjects hmSubject : hmSubjects) {
            SubjectDto subjectDto = new SubjectDto();
            subjectDto.setLable(hmSubject.getSubjectName());
            subjectDto.setValue(hmSubject.getId());
            subjectDtos.add(subjectDto);
        }
        return subjectDtos;
    }

    @Autowired
    private BsUserMapper bsUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result findPage(Integer page, Integer pagesize, String subjectName) {
        IPage<HmSubjects> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<HmSubjects> wrapper = new LambdaQueryWrapper<>();
        if (subjectName != null){
            wrapper.like(HmSubjects::getSubjectName,subjectName);
        }
        Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
        System.out.println("1111111111+"+userId);
        BsUser bsUser = bsUserMapper.selectById(userId);
        System.out.println("99999999999999999"+bsUser);
        IPage<HmSubjects> hmSubjectsIPage = subjectsMapper.selectPage(p,wrapper);
        List<HmSubjects> subjects = hmSubjectsIPage.getRecords();
        for (HmSubjects subject : subjects) {
            subject.setUsername(bsUser.getUsername());
        }

        Result result = new Result();
        result.setCounts(hmSubjectsIPage.getTotal());
        result.setPagesize(pagesize);
        result.setPage(page);
        result.setItems(hmSubjectsIPage.getRecords());
        result.setPages(hmSubjectsIPage.getPages());
        return result;
    }
}
